package com.dzu.gulimall.ware.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dzu.common.utils.PageUtils;
import com.dzu.common.utils.Query;
import com.dzu.common.utils.R;
import com.dzu.gulimall.ware.dao.WareInfoDao;
import com.dzu.gulimall.ware.entity.WareInfoEntity;
import com.dzu.gulimall.ware.feign.MemberFeignService;
import com.dzu.gulimall.ware.service.WareInfoService;
import com.dzu.gulimall.ware.vo.FareVo;
import com.dzu.gulimall.ware.vo.MemberAddressVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Autowired
    private MemberFeignService memberFeignService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareInfoEntity> wareInfoEntityQueryWrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)) {
            wareInfoEntityQueryWrapper.eq("id",key)
                    .or()
                    .like("name",key)
                    .or()
                    .like("address",key)
                    .or()
                    .like("areacode",key);

        }

        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                wareInfoEntityQueryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public FareVo getFare(Long addrId) {
        FareVo vo = new FareVo();

        R r = memberFeignService.addrInfo(addrId);
        MemberAddressVo addressVo = r.getData("data", new TypeReference<MemberAddressVo>() {
        });

        if (addressVo != null) {
            vo.setFare(new BigDecimal(RandomUtil.randomInt(0, 100))); ;
            vo.setAddress(addressVo);
        }
        return vo;
    }
}
